package com.songshuai.webapi.crm.service.impl;

import com.songshuai.webapi.baseapi.dto.feign.response.MasterSourceItemDto;
import com.songshuai.webapi.baseapi.enumerations.FollowStatusEnum;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.enumerations.StudentTypeEnum;
import com.songshuai.webapi.baseapi.enumerations.crm.FileType;
import com.songshuai.webapi.baseapi.enumerations.crm.error.CustomerFollowError;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.baseapi.utils.RegexUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import com.songshuai.webapi.crm.dao.ClueTurnRecordMapper;
import com.songshuai.webapi.crm.dao.CommunicateFileMapper;
import com.songshuai.webapi.crm.dao.CommunicateRecordMapper;
import com.songshuai.webapi.crm.dao.CustomInfoMapper;
import com.songshuai.webapi.crm.dto.request.customerfollow.*;
import com.songshuai.webapi.crm.dto.response.customerfollow.BatchCustomerListResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.CustomArticleResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.ListRecordResDto;
import com.songshuai.webapi.crm.dto.response.customerfollow.MarkingFailureForCustomResDto;
import com.songshuai.webapi.crm.model.ClueTurnRecord;
import com.songshuai.webapi.crm.model.CommunicateFile;
import com.songshuai.webapi.crm.model.CommunicateRecord;
import com.songshuai.webapi.crm.model.CustomInfo;
import com.songshuai.webapi.crm.service.CustomerFollowService;
import com.songshuai.webapi.crm.service.DictionaryService;
import com.songshuai.webapi.crm.util.PageUtil;
import com.songshuai.webapi.crm.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class CustomerFollowServiceImpl extends BaseServiceImpl implements CustomerFollowService {


    @Autowired
    private SnowFlakeUtil snowFlakeUtil;
    @Resource
    CommunicateRecordMapper communicateRecordMapper;
    @Resource
    CommunicateFileMapper communicateFileMapper;
    @Resource
    CustomInfoMapper customInfoMapper;
    @Autowired
    ClueTurnRecordMapper clueTurnRecordMapper;
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    @Transactional
    public void addRecord(AddRecordReqDto addRecordReqDto) {

        addRecordReqDto.setCommunicateBy(this.getCurrentUser().getId()); //当前登录人

        //参数校验
        if(Util.IsNullAndEmpty(addRecordReqDto.getCustomId()) || Util.IsNullAndEmpty(addRecordReqDto.getCommunicateContent())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(addRecordReqDto.getCustomId());
        if(customInfo == null || customInfo.getIsDelete() == 1){
            throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        //是否上传图片
        String[] pictures = null;
        if(!Util.IsNullAndEmpty(addRecordReqDto.getPictures())){
            pictures = addRecordReqDto.getPictures().split(",");

            if(pictures == null || pictures.length == 0){
                throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
            }
        }


       try {

           CommunicateRecord communicateRecord = new CommunicateRecord();
           communicateRecord.setCommunicateId(snowFlakeUtil.nextId());
           PropertiesUtil.copyProperties(addRecordReqDto, communicateRecord);
           communicateRecord.setCustomNo(customInfo.getCustomNo()); //客户编号
           communicateRecord.setCampusId(customInfo.getCampusId()); //校区id
           communicateRecord.setCreateBy(addRecordReqDto.getCommunicateBy()); //创建人
           communicateRecord.setUpdateBy(addRecordReqDto.getCommunicateBy()); //更新人
           communicateRecordMapper.insertSelective(communicateRecord);

           //插入图片
           if(pictures!=null) {

               CommunicateFile communicateFile = null;
               for (String picture : pictures) {
                   communicateFile = new CommunicateFile();
                   communicateFile.setFileId(snowFlakeUtil.nextId());
                   communicateFile.setCampusId(customInfo.getCampusId()); //校区id
                   communicateFile.setCommunicateId(communicateRecord.getCommunicateId()); //沟通记录id
                   communicateFile.setImageAddress(picture); //图片地址
                   communicateFile.setFileType(FileType.PICTURE.getCode()); //文件类型
                   communicateFile.setCreateBy(addRecordReqDto.getCommunicateBy()); //创建人
                   communicateFile.setUpdateBy(addRecordReqDto.getCommunicateBy()); //更新人
                   communicateFileMapper.insertSelective(communicateFile);
               }

           }

           //更新学员主表
           CustomInfo customInfo2 = new CustomInfo();
           Date d = new Date();
           customInfo2.setFollowStatus(FollowStatusEnum.Following.getCode()); //跟进状态：跟进中
           customInfo2.setCustomId(customInfo.getCustomId());//学员ID
           customInfo2.setLastCommunicateTime(d);//最后沟通时间
           customInfo2.setLastCommunicateBy(addRecordReqDto.getCommunicateBy());//最后沟通人
           customInfo2.setNextCommunicateTime(addRecordReqDto.getNextCommunicateTime()); //下次沟通时间
           customInfo2.setLastcommunicateId(communicateRecord.getCommunicateId()); //最后沟通内容id
           customInfo2.setUpdateBy(addRecordReqDto.getCommunicateBy()); //更新人
           customInfo2.setUpdateTime(d);//更新时间
           customInfoMapper.updateByPrimaryKeySelective(customInfo2);

        }catch (Exception e) {
            throw new BusinessException(ResultStatus.ERROR_CREATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }


    }

    @Override
    public PageUtil listRecord(BaseParmDto<ListRecordReqDto> request) {

        //参数校验
        if(Util.IsNullAndEmpty(request.getParam().getCustomId())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(request.getParam().getCustomId());
        if(customInfo == null || customInfo.getIsDelete() == 1){
            throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        try{

            Integer count = communicateRecordMapper.listRecordCount(request.getParam());
            //设置分页
            PageUtil page = new PageUtil(request.getPageIndex(), request.getPageSize(), count);
            request.getParam().setStart(page.getStart());
            request.getParam().setEnd(page.getEnd());

            List<ListRecordResDto> lists = communicateRecordMapper.listRecord(request.getParam());

            //分页沟通人列表
            List<Map> paginationUsersLists = this.getPaginationUsersList(lists);
            //分页文件列表
            List<CommunicateFile> paginationFileLists = this.getPaginationFileList(lists);

            for(ListRecordResDto listRecord:lists){
                listRecord.setCommunicateName(this.getTrueName(listRecord.getCommunicateBy(), paginationUsersLists));
                listRecord.setPictures(this.getPicture(listRecord.getId(), paginationFileLists));
            }

            page.setLists(lists);
            return page;

        }catch (Exception e) {
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }


    }

    @Override
    public List<BatchCustomerListResDto> batchCustomerList(BatchCustomerListReqDto batchCustomerListReqDto) {

        //参数验证
        if(Util.IsNullAndEmpty(batchCustomerListReqDto.getCustomIds())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }
        String[] ids = batchCustomerListReqDto.getCustomIds().split(",");

        if(ids == null || ids.length == 0){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        CustomInfo customInfo = null;
        for(String uid:ids){
            customInfo = customInfoMapper.selectByPrimaryKey(uid);
            if (customInfo == null || customInfo.getIsDelete() == 1) {
                throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
            }
        }

        try{

            batchCustomerListReqDto.setIds(ids);
            List<BatchCustomerListResDto> lists = communicateRecordMapper.batchCustomerList(batchCustomerListReqDto);

            List<Map> paginationUsersList =  this.getPaginationUsersList(lists,1);
            for(BatchCustomerListResDto batchCustomerListResDto:lists){

                batchCustomerListResDto.setOwnerStr(this.getTrueName(batchCustomerListResDto.getOwner(), paginationUsersList));
                batchCustomerListResDto.setFollowStatusStr(FollowStatusEnum.getMsg(batchCustomerListResDto.getFollowStatus()));

            }
            return lists;

        }catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }

    @Override
    @Transactional
    public void batchAssign(BatchAssignReqDto batchAssignReqDto) {


        batchAssignReqDto.setLoginId(this.getCurrentUser().getId()); //当前登录人

        //参数验证
        if(Util.IsNullAndEmpty(batchAssignReqDto.getCustomIds()) || batchAssignReqDto.getOwner() == null){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        String[] ids = batchAssignReqDto.getCustomIds().split(",");

        if(ids == null || ids.length == 0){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        //错误信息
        String errMsg = ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg();

        try {
            ClueTurnRecord clueTurnRecord = null;
            for(String uid:ids){

                CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(uid);
                if (customInfo == null || customInfo.getIsDelete() == 1) {
                    errMsg = CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg();
                    throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
                }

                //跟进人列表-选择未分配
                if(batchAssignReqDto.getOwner() == 0){
                    customInfo.setFollowStatus(FollowStatusEnum.Undistributed.getCode()); //跟进状态：未分配
                    customInfo.setUpdateTime(new Date()); //更新时间
                    customInfo.setUpdateBy(batchAssignReqDto.getLoginId()); //登录人
                    customInfoMapper.updateByPrimaryKeySelective(customInfo);
                }else {

                    //检查是否有重复流转记录
                    ClueTurnRecord clueTurnRecordTemp = new ClueTurnRecord();
                    clueTurnRecordTemp.setCustomId(customInfo.getCustomId());
                    clueTurnRecordTemp.setCurrentOwner(batchAssignReqDto.getOwner());
                    ClueTurnRecord clueTurnRecordTempResulst = clueTurnRecordMapper.selectCustomAndOwner(clueTurnRecordTemp);
                    if(clueTurnRecordTempResulst!=null){
                        errMsg = customInfo.getCustomName()+":"+CustomerFollowError.ERROR_RELATION_IS_EXIST.getMsg();
                        throw new BusinessException(customInfo.getCustomName()+":"+CustomerFollowError.ERROR_RELATION_IS_EXIST.getMsg(), ResultStatus.FAILOfficial);
                    }
                    clueTurnRecord = new ClueTurnRecord();
                    clueTurnRecord.setTurnId(snowFlakeUtil.nextId());
                    clueTurnRecord.setCustomId(customInfo.getCustomId()); //学员ID
                    clueTurnRecord.setCustomNo(customInfo.getCustomNo()); //学员编号
                    clueTurnRecord.setCampusId(customInfo.getCampusId()); //学员校区

                    //上一跟进人
                    ClueTurnRecord clueTurnRecordPre = clueTurnRecordMapper.selectPreOwner(customInfo.getCustomId());
                    if(clueTurnRecordPre!=null){
                        clueTurnRecord.setPreOwner(clueTurnRecordPre.getCurrentOwner());
                    }
                    clueTurnRecord.setCurrentOwner(batchAssignReqDto.getOwner());
                    clueTurnRecord.setCreateBy(batchAssignReqDto.getLoginId());
                    clueTurnRecord.setUpdateBy(batchAssignReqDto.getLoginId());
                    //PropertiesUtil.copyProperties(batchAssignReqDto, clueTurnRecord);
                    clueTurnRecordMapper.insertSelective(clueTurnRecord);

                    customInfo.setOwner(batchAssignReqDto.getOwner()); //所属人
                    customInfo.setUpdateTime(new Date());
                    customInfo.setUpdateBy(batchAssignReqDto.getLoginId());
                    customInfoMapper.updateByPrimaryKeySelective(customInfo);
                }
            }
        }catch (Exception e){
            throw new BusinessException(errMsg, ResultStatus.FAILOfficial);
        }
    }

    @Override
    @Transactional
    public void deleteCustom(DeleteCustomReqDto deleteCustomReqDto) {

        deleteCustomReqDto.setLoginId(this.getCurrentUser().getId());
        //参数验证
        if(Util.IsNullAndEmpty(deleteCustomReqDto.getCustomIds())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        String[] ids = deleteCustomReqDto.getCustomIds().split(",");

        if(ids == null || ids.length == 0){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        String errMsg = ResultStatus.ERROR_DELETE_FAILED.getErrorMsg();
        try {

            for (String uid : ids) {
                CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(uid);
                if (customInfo == null || customInfo.getIsDelete() == 1) {
                    errMsg = CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg();
                    throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
                }
                customInfo.setIsDelete(1);
                customInfo.setUpdateBy(deleteCustomReqDto.getLoginId());//更新人
                customInfo.setUpdateTime(new Date()); //更新时间
                customInfoMapper.updateByPrimaryKeySelective(customInfo);

            }

        }catch (Exception e){
            throw new BusinessException(errMsg, ResultStatus.FAILOfficial);
        }

    }





    /*分页文件列表*/
    private List<CommunicateFile> getPaginationFileList(List<ListRecordResDto> lists){

        if(lists == null || lists.size() == 0){
            return null;
        }
        StringBuffer bf = new StringBuffer();
        for(ListRecordResDto listRecordResDto:lists){
            bf.append(",");
            bf.append(listRecordResDto.getId());
        }

        String[] fileIds = bf.substring(1).split(",");

        return communicateFileMapper.selectFileListForStr(fileIds);
    }

    /*分页跟进人列表*/
    private List<Map> getPaginationUsersList(List<BatchCustomerListResDto> lists, int status){

        if(lists == null || lists.size() == 0){
            return null;
        }
        StringBuffer bf = new StringBuffer();
        for(BatchCustomerListResDto batchCustomerListResDto:lists){
            bf.append(",");
            bf.append(batchCustomerListResDto.getOwner());
        }
        QueryResponse qr = permissionClient.queryUsersForStr(getBearerToken(), bf.substring(1));
        if(qr.getCode() == 1){
            return null;
        }
        return (List<Map>)qr.getData();
    }
    /*分页沟通人列表*/
    private List<Map> getPaginationUsersList(List<ListRecordResDto> lists){

        if(lists == null || lists.size() == 0){
            return null;
        }
        StringBuffer bf = new StringBuffer();
        for(ListRecordResDto listRecordResDto:lists){
            bf.append(",");
            bf.append(listRecordResDto.getCommunicateBy());
        }
        QueryResponse qr = permissionClient.queryUsersForStr(getBearerToken(), bf.substring(1));
        if(qr.getCode() == 1){
            return null;
        }
        return (List<Map>)qr.getData();
    }

    /*返回操作人真实姓名*/
    private String getTrueName(Integer id, List<Map> lists){

        if(lists == null) {
            return "";
        }

        String trueName = "";
        for(Map map:lists){
            if(Integer.parseInt(map.get("id").toString()) == id){
                trueName = map.get("trueName").toString();
                break;
            }
        }
        return trueName;
    }

    /*返回记录图片*/
    private List<Map> getPicture(String id, List<CommunicateFile> paginationFileLists){
        List<Map> lists  = new ArrayList();
        Map map = null;
        for(CommunicateFile communicateFile:paginationFileLists){
            if(communicateFile.getCommunicateId().equals(id)){
                map = new HashMap();
                map.put("url",communicateFile.getImageAddress());
                lists.add(map);
            }
        }
        return lists;
    }

    @Override
    @Transactional
    public MarkingFailureForCustomResDto markingFailureForCustom(MarkingFailureForCustomReqDto markingFailureForCustomReqDto) {

        markingFailureForCustomReqDto.setLoginId(this.getCurrentUser().getId());

        //当学员类型为“潜在学员”，且跟进状态不为“已失效”时，显示该按钮
        //学员的跟进状态，变更为“已失效”，跟进记录中新增一条记录

        //参数检查
        if(markingFailureForCustomReqDto.getStatus() == null || Util.IsNullAndEmpty(markingFailureForCustomReqDto.getCustomId())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(markingFailureForCustomReqDto.getCustomId());
        if(customInfo == null || customInfo.getIsDelete() == 1){
            throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        MarkingFailureForCustomResDto result = new MarkingFailureForCustomResDto();
        result.setIsShow(false);

        try{

            //检查按钮是否显示
            if(customInfo.getCustomType() == StudentTypeEnum.Potential.getCode() && customInfo.getFollowStatus() != FollowStatusEnum.Invalid.getCode()){
                result.setIsShow(true);
            }

            if(markingFailureForCustomReqDto.getStatus() == 1){

                if(result.getIsShow() == false){
                    throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
                }
                //状态为1,执行失效操作
                customInfo.setFollowStatus(FollowStatusEnum.Invalid.getCode());
                customInfo.setUpdateTime(new Date()); //更新时间
                customInfo.setUpdateBy(markingFailureForCustomReqDto.getLoginId()); //更新人
                customInfoMapper.updateByPrimaryKeySelective(customInfo);

                String content = FollowStatusEnum.Invalid.getMsg();
                if(!Util.IsNullAndEmpty(markingFailureForCustomReqDto.getContent())){
                    content = markingFailureForCustomReqDto.getContent();
                }

                //添加失效沟通记录
                CommunicateRecord communicateRecord = new CommunicateRecord();
                communicateRecord.setCommunicateId(snowFlakeUtil.nextId());
                communicateRecord.setCustomId(markingFailureForCustomReqDto.getCustomId()); //学员ID
                communicateRecord.setCustomNo(customInfo.getCustomNo()); //学员编号
                communicateRecord.setCampusId(customInfo.getCampusId()); //校区
                communicateRecord.setCommunicateBy(markingFailureForCustomReqDto.getLoginId()); //登录人
                communicateRecord.setCommunicateContent(content); //沟通内容
                communicateRecord.setCreateBy(markingFailureForCustomReqDto.getLoginId()); //创建人
                communicateRecord.setUpdateBy(markingFailureForCustomReqDto.getLoginId()); //更新人
                communicateRecordMapper.insertSelective(communicateRecord);

            }

            return result;

        }catch (Exception e){
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }

    }


    @Override
    public CustomArticleResDto customArticle(CustomArticleReqDto customArticleReqDto) {

        //参数检查
        if(Util.IsNullAndEmpty(customArticleReqDto.getCustomId())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        CustomInfo customInfo = customInfoMapper.selectByPrimaryKey(customArticleReqDto.getCustomId());
        if(customInfo == null || customInfo.getIsDelete() == 1){
            throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        try {
            CustomArticleResDto customArticleResDto = new CustomArticleResDto();
            PropertiesUtil.copyProperties(customInfo, customArticleResDto);

            //设置来源
            customArticleResDto.setFirstSourceStr("");
            List<MasterSourceItemDto> firstSourceList = dictionaryService.querySourceList();

            for(MasterSourceItemDto masterSourceItemDto:firstSourceList){
                if(masterSourceItemDto.getSourceId().intValue() == customArticleResDto.getFirstSource().intValue()){
                    customArticleResDto.setFirstSourceStr(masterSourceItemDto.getChannelName());
                    break;
                }
            }


            return customArticleResDto;

        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }

    @Override
    public void editCustomArticle(EditCustomArticleReqDto editCustomArticleReqDto) {

        //参数检查
        if(Util.IsNullAndEmpty(editCustomArticleReqDto.getCustomId()) ||Util.IsNullAndEmpty(editCustomArticleReqDto.getCustomName()) || Util.IsNullAndEmpty(editCustomArticleReqDto.getPhone())){
            throw new BusinessException(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorMsg(), ResultStatus.FAILOfficial);
        }

        if(!RegexUtil.isMobile(editCustomArticleReqDto.getPhone())){
            throw new BusinessException(CustomerFollowError.ERROR_MOBILE_FORMAT.getMsg(), ResultStatus.FAILOfficial);
        }

        CustomInfo customInfoCheck = customInfoMapper.selectByPrimaryKey(editCustomArticleReqDto.getCustomId());
        if(customInfoCheck == null || customInfoCheck.getIsDelete() == 1){
            throw new BusinessException(CustomerFollowError.ERROR_CUSTOM_ID_NOT_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        //第一手机号,同一校区不可重复
        CustomInfo info = new CustomInfo();
        info.setCustomId(editCustomArticleReqDto.getCustomId());
        info.setPhone(editCustomArticleReqDto.getPhone());
        info.setCampusId(customInfoCheck.getCampusId());
        if(customInfoMapper.selectByPhone(info)!=null){
            throw new BusinessException(CustomerFollowError.ERROR_PHONE_IS_EXIST.getMsg(), ResultStatus.FAILOfficial);
        }

        try{
            CustomInfo customInfo = new CustomInfo();
            customInfo.setUpdateTime(new Date()); //更新时间
            customInfo.setUpdateBy(this.getCurrentUser().getId()); //更新人

            PropertiesUtil.copyProperties(editCustomArticleReqDto, customInfo);
            Integer result = customInfoMapper.updateByPrimaryKeySelective(customInfo);
            if(result.intValue() <=0){
                throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
            }

        }catch (Exception e){
            throw new BusinessException(ResultStatus.ERROR_UPDATE_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }

    }

    @Override
    public List<Map> batchUserList(BatchUserListReqDto batchUserListReqDto) {


        batchUserListReqDto.setCampusId(this.getCurrentUser().getCampusId()); //校区

        try{

            List<Map> lists = new ArrayList();
            Map map = null;
            QueryResponse qr = permissionClient.getUsersByCampusId(getBearerToken(), batchUserListReqDto.getCampusId(),false);
            if(qr.getData()!=null){
                for(Map m:(List<Map>)qr.getData()){
                    map = new HashMap();

                    map.put("id",m.get("user_id").toString());
                    map.put("name","");
                    if(m.get("true_name")!=null){
                        map.put("name",m.get("true_name").toString());
                    }

                    lists.add(map);
                }
            }
            return lists;

        }catch (Exception e){
            throw new BusinessException(ResultStatus.ERROR_SEARCH_FAILED.getErrorMsg(), ResultStatus.FAILOfficial);
        }
    }
}
